package com.hanqi.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanqi.service.SysRoleFuncService;
import com.hanqi.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysRoleFunc")
public class SysRoleFuncController {

    @Autowired
    protected SysRoleFuncService sysRoleFuncService;

    @ResponseBody
    @RequestMapping("/funcsByMenuIdWithRoleId")
    public JSONObject funcsByMenuIdWithRoleId(String menuId, String roleId) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (StringUtils.isEmpty(menuId)) {
            return R.msg(true, "", list);
        }
        list = sysRoleFuncService.funcsByMenuIdWithRoleId(menuId, roleId);
        return R.msg(true, "", list);
    }

    @ResponseBody
    @RequestMapping("/updateAuth")
    public JSONObject updateAuth(String menuFuncId, String roleId, boolean opera) {
        if (opera) {
            sysRoleFuncService.insertAuth(menuFuncId, roleId);
        } else {
            sysRoleFuncService.removeAuth(menuFuncId, roleId);
        }
        return R.SUCCESS;
    }
}
